package studio7Omega.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import studio7Omega.dao.ItemDAO;
import studio7Omega.dao.LocalDAO;
import studio7Omega.dao.SalaDAO;
import studio7Omega.excepcion.DAOExcepcion;
import studio7Omega.modelo.Instrumento;
import studio7Omega.modelo.Local;
import studio7Omega.modelo.Sala;
import studio7Omega.modelo.Servicio;
import studio7Omega.negocio.SistemaNegocio;

@WebServlet(name = "ServletSala", urlPatterns = {
    "/armadoSala",
    "/guardarArmadoSala",
    "/listarServicios",
    "/listarInstrumentos",
    "/listarSala",
    "/obtenerSala",
    "/agregarItem",
    "/eliminarItem",
    "/obtenerSalaCosto"
})
public class ServletSala extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        try {
            String path = request.getServletPath();
            if (path.equals("/armadoSala")) {
                Collection<Local> colLocal = new ArrayList<Local>();
                colLocal = LocalDAO.obtenerLocal();
                session.setAttribute("colLocal", colLocal);
                RequestDispatcher rd = request.getRequestDispatcher("armadoSala.jsp");
                rd.forward(request, response);
            } else if (path.equals("/listarSala")) {
                int idlocal = Integer.parseInt(request.getParameter("idlocal"));
                Collection<Sala> colSala = new ArrayList<Sala>();
                colSala = SalaDAO.obtenerSalasLocal(idlocal);
                out.println("<select name='cbSala' id='cbSala' onchange='getItems(this.value);getSalaCosto(this.value)'>"
                        + "<option selected='selected'>Seleccionar</option>");
                for (Sala sala : colSala) {
                    out.println("<option value='" + sala.getIdSala() + "'>" + sala.getDescripcion() + "</option>");
                }
            } else if (path.equals("/obtenerSalaCosto")) {
                int idsala = Integer.parseInt(request.getParameter("idsala"));
                Sala sala = SalaDAO.buscarSalaPorId(idsala);
                out.println(sala.getCosto());
            } else if (path.equals("/obtenerSala")) {
                int idsala = Integer.parseInt(request.getParameter("idsala"));
                Sala sala = SalaDAO.buscarSalaPorId(idsala);
                ArrayList<Instrumento> colInstrumentos = (ArrayList<Instrumento>) sala.getInstrumento();
                ArrayList<Servicio> colServicios = (ArrayList<Servicio>) sala.getServicio();
                out.println("<table width='700' border='1'><tr><td width='250'><strong>Item</strong></td>");
                out.println("<td width='100'><strong>Tipo</strong></td><td width='150'><strong>Costo</strong></td><td width='100'><strong>Eliminar</strong></td></tr>");

                for (Instrumento ins : colInstrumentos) {
                    out.println("<tr><td>" + ins.getDescripcion() + "</td>");
                    out.println("<td>" + ins.getTipo() + "</td>");
                    out.println("<td>" + ins.getCosto() + "</td>");
                    out.println("<td><a href=javascript:eliminarItem("+idsala + "," + ins.getIdItem() + ")>Eliminar</a></td></tr>");
                }
                for (Servicio ser : colServicios) {
                    out.println("<tr><td>" + ser.getDescripcion() + "</td>");
                    out.println("<td>" + ser.getTipo() + "</td>");
                    out.println("<td>" + ser.getCosto() + "</td>");
                    out.println("<td><a href=javascript:eliminarItem("+idsala + "," + ser.getIdItem() + ")>Eliminar</a></td></tr>");
                   
                }
                out.println("</table>");
            } else if (path.equals("/listarInstrumentos")) {
                Collection<Instrumento> instrumentos = new ArrayList<Instrumento>();
                instrumentos = ItemDAO.listarInstrumento();
                out.println("<select name='cbItems' id='cbItems'>"
                        + "<option selected='selected' value=0>Seleccionar</option>");
                for (Instrumento ins : instrumentos) {
                    out.println("<option value='" + ins.getIdItem() + "'>" + ins.getDescripcion() + "</option>");
                }
            } else if (path.equals("/listarServicios")) {
                Collection<Servicio> servicios = new ArrayList<Servicio>();
                int idsala = Integer.parseInt(request.getParameter("idsala"));
                servicios = ItemDAO.listarServicio(idsala);
                out.println("<select name='cbItems' id='cbItems'>"
                        + "<option selected='selected' value=0>Seleccionar</option>");
                for (Servicio ser : servicios) {
                    out.println("<option value='" + ser.getIdItem() + "'>" + ser.getDescripcion() + "</option>");
                }
            } else if (path.equals("/agregarItem")) {
                int iditem = Integer.parseInt(request.getParameter("iditem"));
                int idsala = Integer.parseInt(request.getParameter("idsala"));
                String mensaje = SistemaNegocio.agregarSalaItem(idsala, iditem);
                out.println(mensaje);
            } else if (path.equals("/eliminarItem")) {
                int iditem = Integer.parseInt(request.getParameter("iditem"));
                int idsala = Integer.parseInt(request.getParameter("idsala"));
                String mensaje = SistemaNegocio.eliminarSalaItem(idsala, iditem);              
                out.println(mensaje);
            } else if (path.equals("/guardarArmadoSala")) {
                int codsala = Integer.parseInt(request.getParameter("cbSala"));
                int codinst = Integer.parseInt(request.getParameter("cbInstrumento"));
                Sala sala = new SalaDAO().buscarSalaPorId(codsala);
                Instrumento ins = new Instrumento();
                ins.setIdItem(codinst);
                Collection<Instrumento> lista = new ArrayList<Instrumento>();
                lista.add(ins);
                sala.setInstrumento(lista);
            }
        } catch (DAOExcepcion ex) {
            System.out.println(ex.getMessage());


        } finally {
            out.close();


        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";

    }// </editor-fold>
}
